package com.yyfax.pay.order.data.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yyfax.commons.lang.DateUtil;
import com.yyfax.framework.service.CacheService;
import com.yyfax.framework.service.impl.BaseServiceImpl;
import com.yyfax.pay.order.data.dao.NotifyTaskDao;
import com.yyfax.pay.order.data.service.NotifyTaskService;
import com.yyfax.pay.order.model.po.NotifyTaskPO;

/**
 * 异步通知任务,用于异步通知业务模块服务实现
 *
 * @author buyi
 * @date 2017-04-12 11:09:15
 * @since 1.0.0
 */
@Service
public class NotifyTaskServiceImpl extends BaseServiceImpl implements NotifyTaskService {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private NotifyTaskDao notifyTaskDao;

	public NotifyTaskServiceImpl(@Qualifier("payCacheService") CacheService cacheService) {
		super(cacheService);
	}

	@Override
	public NotifyTaskPO queryByIdAndMerchantCode(int id, String merchantCode) throws Exception {
		if (id <= 0 && StringUtils.isBlank(merchantCode)) {
			logger.debug("参数 'id' 不能为0，且 'merchantCode' 不能为空 ");
			return null;
		}

		return notifyTaskDao.selectByIdAndMerchantCode(id, merchantCode);
	}

	@Override
	public int save(NotifyTaskPO po) throws Exception {
		if (po == null) {
			logger.debug("参数po为空，返回-1");
			return 0;
		}

		po.setCreateTime(DateUtil.getCurrentDateTime());
		int id = notifyTaskDao.insert(po);
		if (id > 0) {
			po.setId(id);
		}

		return id;
	}

	// @Override
	// public void updateTask(NotifyTaskPO task) throws Exception {
	// if (task == null) {
	// return;
	// }
	// notifyTaskDao.update(task);
	// }

	@Override
	public void updateTaskByMerchantAndOrderId(NotifyTaskPO task) throws Exception {
		if (task == null || StringUtils.isBlank(task.getMerchantCode()) || StringUtils.isBlank(task.getOrderId())) {
			return;
		}

		notifyTaskDao.updateByMerchantAndOrderId(task);
	}

	@Override
	public void updateState(NotifyTaskPO task) {
		
		if (task == null || StringUtils.isBlank(task.getMerchantCode()) || StringUtils.isBlank(task.getOrderId())) {
			return;
		}

		notifyTaskDao.updateState(task);

	}
}
